package cn.guxinhu.billsystem.user.controller;

import cn.guxinhu.billsystem.user.pojo.BillUser;
import cn.guxinhu.billsystem.user.pojo.BillUserQueryVo;
import cn.guxinhu.billsystem.user.service.impl.UserLoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author Guxinhu
 * @create 2021-06-21 14:31
 */
@Controller
public class UserLoginController {
    /**
     * @Description: 登录的控制层
     * @Method: LoginContrller.login()
     * @Param: /login -- 网页映射路径
     * user(User) -- 数据库实体类
     * userQueryVo(UserQueryVo) -- 前端传入的实体类
     * @Return: [modelAndView(ModelAndView)]
     * @Author: Guxinhu
     * @Date Created in 2020/08/19 11:37
     */
    @Autowired
    private BillUser user;
    @Autowired
    private UserLoginServiceImpl userLoginService;


    @RequestMapping("/user/login")
    public ModelAndView login(BillUserQueryVo userQueryVo, HttpSession session, ModelAndView modelAndView) {
        user = (BillUser) session.getAttribute("user");
        if (user == null) {
            try {
                user = userLoginService.login(userQueryVo.getUserName());
                if (user.getPassWord().equals(userQueryVo.getPassWord())) {
                    session.setAttribute("user", user);
                    modelAndView.setViewName("/common/index");
                    System.out.println("1:" + user);
                }
            } catch (Exception e) {
                modelAndView.setViewName("/user/mian");
                session.invalidate();
                modelAndView.addObject("mes", "密码或用户名错误");
            }
        }
        return modelAndView;
    }
}
